package com.yhp.web;

import com.yhp.bean.Menu;
import com.yhp.bean.Role;
import com.yhp.service.impl.MenuServiceImpl;
import com.yhp.service.impl.RoleServletImpl;
import com.yhp.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/power/role/roles")
public class RoleServlet extends HttpServlet {
    RoleServletImpl roleServlet = new RoleServletImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if("get".equals(method)){
            //查看所有角色
            GetRole(req,resp);
        }else if("ord".equals(method)){
            //开启关闭
            openDown(req,resp);
        }else if("add".equals(method)){
            //添加信息
            roleAdd(req,resp);

        }else if("update".equals(method)){
            //修改信息
            updateRole(req,resp);

        }else if("delete".equals(method)){
            //删除信息
            roleDelete(req,resp);

        }else if("menuAdd".equals(method)){
            //跳转add页面  添加页面+获取菜单列表
            menuAdd(req,resp);
        }else if("edit".equals(method)){
            //跳转edit页面 修改页面+获取菜单列表
            menuEdit(req,resp);
        }else if("info".equals(method)){
            menuInfo(req,resp);
        }
    }

    //protected void GetRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{}

    //跳转info页面
    protected void menuInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Role byId = roleServlet.findById(Integer.parseInt(id));
        req.setAttribute("byid",byId);
        req.setAttribute("byidList",byId.getMenuList());
        req.getRequestDispatcher("info.jsp").forward(req,resp);


    }


    //修改信息
    protected void updateRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String roleName = req.getParameter("roleName");
        String[] menuIds = req.getParameterValues("menuId");
        String roleState = req.getParameter("roleState");
        Role role = new Role();
        role.setRoleId(Integer.parseInt(id));
        role.setRoleName(roleName);
        role.setRoleState(Integer.parseInt(roleState));
        int i = roleServlet.updateRole(role, menuIds);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('修改成功');location.href='/power/role/roles?method=get';</script>");
        }else {
            writer.println("<script>alert('修改失败');location.href='/power/role/roles?method=get';</script>");

        }


    }


    //跳转edit页面
    protected void menuEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Role byId = roleServlet.findById(Integer.parseInt(id));
        List<Menu> menuList1 = byId.getMenuList();
        for (Menu menu : menuList1) {
            System.out.println(menu.getMenuId());
            System.out.println(menu.getMenuName());
        }
        System.out.println("----------------");
        req.setAttribute("byid",byId);
        List<Menu> successList = menuList1.get(0).getSuccessList();
        for (Menu menu : successList) {
            System.out.println(menu.getMenuId());
            System.out.println(menu.getMenuName());
        }
        List<Menu> successList1 = successList.get(0).getSuccessList();


        MenuServiceImpl menuService = new MenuServiceImpl();
        List<Menu> menuList = menuService.getMenuList();

        req.setAttribute("menuList",menuList);
        req.getRequestDispatcher("edit.jsp").forward(req,resp);
    }

    //删除角色
    protected void roleDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        int i = roleServlet.deleteRole(Integer.parseInt(id));
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('删除成功');location.href='/power/role/roles?method=get';</script>");
        }else {
            writer.println("<script>alert('删除失败');location.href='/power/role/roles?method=get';</script>");

        }


    }


    //添加角色
    protected void roleAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String roleName = req.getParameter("roleName");
        String[] menuIds = req.getParameterValues("menuId");
        String roleState = req.getParameter("roleState");
        Role role = new Role();
        role.setRoleName(roleName);
        role.setRoleState(Integer.parseInt(roleState));
        int i = roleServlet.addRole(role, menuIds);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('添加成功');location.href='/power/role/roles?method=get';</script>");
        }else {
            writer.println("<script>alert('添加失败');location.href='/power/role/roles?method=get';</script>");

        }


    }
    //查看所有角色
    protected void GetRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageIndex = req.getParameter("pageIndex");

        int index = pageIndex==null?1:Integer.parseInt(pageIndex);

        PageUtil pageUtil = new PageUtil();
        List<Role> role = roleServlet.getRole(index,pageUtil.getPageSize());
        int total = roleServlet.total();
        pageUtil.setTotal(total);
        pageUtil.setDataList(role);
        pageUtil.setPageIndex(index);
        req.setAttribute("pi",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }

    //开启关闭
    protected void openDown(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleState = req.getParameter("roleState");
        int state = Integer.parseInt(roleState);
        if(state==0){
            state=1;
        }else if(state==1){
            state=0;
        }
        String roleId = req.getParameter("roleId");
        System.out.println(state);
        System.out.println(roleId);
        int ord = roleServlet.ord(Integer.parseInt(roleId), state);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(ord>0){
            writer.println("<script>alert('操作成功');location.href='/power/role/roles?method=get'</script>");

        }else{
            writer.println("<script>alert('操作失败');location.href='/power/role/roles?method=get'</script>");

        }
    }

    //跳转add  获取菜单列表
    protected void menuAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        MenuServiceImpl menuService = new MenuServiceImpl();
        List<Menu> menuList = menuService.getMenuList();
        req.setAttribute("menuList",menuList);
        req.getRequestDispatcher("add.jsp").forward(req,resp);
    }



}
